package com.zabra.zl.ui.bargain.tab.history;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpFragment;
import com.zabra.zl.data.OrderListBean;
import com.zabra.zl.myview.state_layout.OtherView;
import com.zabra.zl.ui.bargain.tab.history.contract.TabHistoryContract;
import com.zabra.zl.ui.bargain.tab.history.presenter.TabHistoryPresenter;
import com.zabra.zl.utils.DateUtils;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.SelectorUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

@SuppressLint("ValidFragment")
public class TabHistory extends BaseMvpFragment<TabHistoryPresenter> implements TabHistoryContract.View {
    @BindView(R.id.tv_startdata)
    TextView tvStartdata;
    @BindView(R.id.ll_startdate)
    LinearLayout llStartdate;
    @BindView(R.id.tv_enddata)
    TextView tvEnddata;
    @BindView(R.id.ll_enddate)
    LinearLayout llEnddate;
    @BindView(R.id.recycler_title)
    RecyclerView recyclerTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.otherview)
    OtherView otherview;
    private List<String> titleList = new ArrayList<>();
    private List<OrderListBean.RespDataBean> list;
    private AdpTitle adpTitle;
    private Adp adp;
    private int orderId;
    private String startTime, endTime;
    private Dialog dialog;
    private TimePickerView pvCustomTime;

    public TabHistory(int orderId) {
        this.orderId = orderId;
    }

    @Override
    protected void initView(View view) {
        otherview.setHolder(mHolder);
        mPresenter = new TabHistoryPresenter();
        mPresenter.attachView(this);
        mPresenter.getData(5, orderId, startTime, endTime);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tab_history_bargain;
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(getActivity());
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(OrderListBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            list = bean.getRespData();
            if (list.size() == 0) {
                otherview.showEmptyView();
            } else {
                otherview.showContentView();
                //右边内容
                recycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
                recycler.setNestedScrollingEnabled(false);
                adp = new Adp(R.layout.item_bargain, list);
                recycler.setAdapter(adp);
                adp.openLoadAnimation();
                titleList.clear();
                for (OrderListBean.RespDataBean respDataBean : list) {
                    titleList.add(respDataBean.getSharesCode());
                }
                //左边标签
                recyclerTitle.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
                recyclerTitle.setNestedScrollingEnabled(false);
                adpTitle = new AdpTitle(R.layout.item_bargain_title, titleList);
                recyclerTitle.setAdapter(adpTitle);
                adpTitle.openLoadAnimation();
            }

        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(getActivity());
        } else {
            ToastUtil.showToast(getActivity(), bean.getRespDesc());
        }
    }


    @OnClick({R.id.ll_startdate, R.id.ll_enddate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_startdate:
                initCustomTimePickerStart();
                break;
            case R.id.ll_enddate:
                initCustomTimePickerEnd();
                break;
        }
    }

    public void initCustomTimePickerStart() {
        //时间选择器 ，自定义布局
        pvCustomTime = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                tvStartdata.setText(DateUtils.getHM(date, "yyyyMMdd"));
                startTime = DateUtils.getHM(date, "yyyy/MM/dd");
                mPresenter.getData(5, orderId, startTime, endTime);
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView ivCancel = v.findViewById(R.id.tv_cancle);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startTime = "";
                                tvStartdata.setText("全部");
                                mPresenter.getData(5, orderId, startTime, endTime);
                                pvCustomTime.dismiss();
                            }
                        });
                    }
                })
                .setContentTextSize(18)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(1.2f)
                .setTextXOffset(0, 0, 0, 40, 0, -40)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(Color.parseColor("#999999"))
                .build();
        pvCustomTime.show();
    }

    public void initCustomTimePickerEnd() {
        //时间选择器 ，自定义布局
        pvCustomTime = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                tvEnddata.setText(DateUtils.getHM(date, "yyyyMMdd"));
                endTime = DateUtils.getHM(date, "yyyy/MM/dd");
                mPresenter.getData(5, orderId, startTime, endTime);
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView ivCancel = v.findViewById(R.id.tv_cancle);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                endTime = "";
                                tvEnddata.setText("全部");
                                mPresenter.getData(5, orderId, startTime, endTime);
                                pvCustomTime.dismiss();
                            }
                        });
                    }
                })
                .setContentTextSize(18)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(1.2f)
                .setTextXOffset(0, 0, 0, 40, 0, -40)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(Color.parseColor("#999999"))
                .build();
        pvCustomTime.show();
    }

}
